<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="商品编码：" prop="skuCode">
        <el-input
          v-model="queryParams.skuCode"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
      <!--        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">申请仓储</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['system:warehousInfo:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="productList" border align="center">
      <el-table-column label="商品编码" align="center" prop="skuCode" min-width="200"/>
      <el-table-column label="商品图片" align="center">
        <template slot-scope="scope">
          <image-preview :src="scope.row.skuImage" width="100px" height="100px"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="可用库存量" align="center" prop="availableStock"/>
      <el-table-column label="待出库存量" align="center" prop="pendingStock"/>
      <el-table-column label="在途库存量" align="center" prop="onwayStock"/>
      <el-table-column label="价格（美元）" align="center" prop="skuPrice"/>
      <!--      <el-table-column label="操作" align="center">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button size="mini" @click="handleAdd(scope.row)" type="text">入仓</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改FPX仓储信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="applyForm" :model="applyForm" :rules="rules" label-width="120px">
        <el-form-item label="商品数量" prop="productCount">
          <el-input v-model="applyForm.productCount" placeholder="请输入商品数量"/>
        </el-form-item>
        <el-form-item label="国内运单号" prop="trackingNo">
          <el-input v-model="applyForm.trackingNo" placeholder="请输入国内运单号"/>
        </el-form-item>
        <el-form-item label="预计到货时间" prop="arrivalTime">
          <el-date-picker clearable v-model="applyForm.arrivalTime" type="date" value-format="yyyy-MM-dd"
                          placeholder="请选择预计到货时间"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUserProduct,
  addWarehousInfo, getProductCode,
} from "@/api/warehouse/warehouse";

export default {
  name: "WarehousInfo",
  dicts: ['erp_currency_code'],
  data() {
    return {
      buttonLoading: false,// 按钮loading
      loading: true,// 遮罩层
      ids: [], // 选中数组
      single: true,// 非单个禁用
      multiple: true, // 非多个禁用
      showSearch: true,// 显示搜索条件
      total: 0, // 总条数
      productList: [],// FPX仓储信息表格数据
      title: "",  // 弹出层标题
      open: false, // 是否显示弹出层
      queryParams: {   // 查询参数
        pageNum: 1,
        pageSize: 10,
        skuCode: undefined,
      },
      applyForm: {}, // 表单参数
      skuCode: "",
      rules: {    // 表单校验
        skuCode: [{required: true, message: "商品编码不能为空", trigger: "blur"}],
        productCount: [{required: true, message: "商品数量不能为空", trigger: "blur"}],
        productName: [{required: true, message: "商品名称不能为空", trigger: "blur"}],
        productUnitPrice: [{required: true, message: "商品单价不能为空", trigger: "blur"}],
        productUnitCurrencyCode: [{required: true, message: "币种不能为空", trigger: "blur"}],
        arrivalTime: [{required: true, message: "预计到货时间不能为空", trigger: "blur"}],
        trackingNo: [{required: true, message: "国内运单号不能为空", trigger: "blur"}],
      }
    };
  },
  created() {
    this.userInfo = this.$store.state.user.urserInfo
    this.getList();
  },
  methods: {
    /** 查询FPX仓储信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.userId = this.userInfo.userId
      getUserProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        // this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.applyForm = {
        productCount: undefined,
        productName: undefined,
        productUnitCurrencyCode: undefined,
        productUnitPrice: undefined,
        arrivalTime: undefined,
        trackingNo: undefined,
      };
      this.resetForm("applyForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 申请仓储 */
    handleAdd(row) {
      this.reset();
      this.open = true;
      this.title = "添加仓储信息";
      this.skuCode = row.skuCode
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["applyForm"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.applyForm.skuCode = this.skuCode
          this.applyForm.arrivalTime = new Date(this.applyForm.arrivalTime).getTime()
          addWarehousInfo(this.applyForm).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          })
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/warehousInfo/export', {
        ...this.queryParams
      }, `warehousInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
